import { http } from "@/utils/http";

// 供应商基本信息接口
export interface VendorItem {
  id: number;
  name: string; // 供应商名称
  person: string; // 联系人
  phone: string; // 联系电话
  email: string; // 邮箱
  address: string; // 地址
  remark?: string; // 备注
}

// 查询供应商列表参数
export interface VendorListParams {
  page?: number;
  limit?: number;
  name?: string;
}

// 查询供应商列表返回结果
export interface VendorListResult {
  code: number;
  data: {
    records: VendorItem[];
    pageNum: number;
    pageSize: number;
    totalRow: number;
    totalPage: number;
  };
  message: string;
}

// 添加/修改供应商参数
export interface VendorPayload {
  name: string;
  person: string;
  phone: string;
  email: string;
  address: string;
  remark?: string;
}

// 创建供应商返回结果
export interface VendorCreateResult {
  code: number;
  data: number;
  message: string;
}

// 更新供应商返回结果
export interface VendorUpdateResult {
  code: number;
  data: boolean;
  message: string;
}

// 删除供应商返回结果
export interface VendorDeleteResult {
  code: number;
  data: boolean;
  message: string;
}

// 批量删除供应商返回结果
export interface VendorBatchDeleteResult {
  code: number;
  data: object;
  message: string;
}

// 查询单个供应商返回结果
export interface VendorDetailResult {
  code: number;
  data: VendorItem;
  message: string;
}

// 查询所有供应商返回结果
export interface VendorAllListResult {
  code: number;
  data: object;
  message: string;
}

/**
 * 查询供应商列表
 * @param data 查询参数
 * @returns 供应商列表数据
 */
export const getVendorList = (data: VendorListParams) => {
  return http.request<VendorListResult>("post", "vendor/list", { data });
};

/**
 * 添加供应商
 * @param data 供应商信息
 * @returns 创建结果
 */
export const createVendor = (data: VendorPayload) => {
  return http.request<VendorCreateResult>("post", "vendor/add", { data });
};

/**
 * 修改供应商信息
 * @param data 供应商信息（包含id）
 * @returns 修改结果
 */
export const updateVendor = (data: VendorPayload & { id: number }) => {
  return http.request<VendorUpdateResult>("post", "vendor/update", { data });
};

/**
 * 根据ID查询供应商信息
 * @param id 供应商ID
 * @returns 供应商详细信息
 */
export const getVendorById = (id: number) => {
  return http.request<VendorDetailResult>("get", "vendor/find", { params: { id } });
};

/**
 * 删除供应商信息
 * @param id 供应商ID
 * @returns 删除结果
 */
export const deleteVendor = (id: number) => {
  return http.request<VendorDeleteResult>("get", "vendor/delete", { params: { id } });
};

/**
 * 批量删除供应商信息
 * @param data 包含id数组的请求体
 * @returns 删除结果
 */
export const deleteBatchVendor = (data: { ids: number[] }) => {
  return http.request<VendorBatchDeleteResult>("post", "vendor/deleteBatch", { data });
};

/**
 * 查询所有供应商信息
 * @returns 所有供应商数据
 */
export const findAllVendor = () => {
  return http.request<VendorAllListResult>("get", "vendor/findAll");
};